<template>
  <div id="roles" >
    <!-- 面包导航 -->
    <el-breadcrumb separator="/" style="padding-left:10px;padding-bottom:10px;font-size:12px;">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片主体 -->
    <el-card class="box-card">
      <div class="block">
        <!-- 节点过滤 -->
        <el-row>
          <el-col :span="16">
            <div class="grid-content bg-purple">
              <el-input placeholder="输入关键字进行过滤"  clearable></el-input>
            </div>
          </el-col>
          <el-col :span="2">
            <div class="grid-content bg-purple-light">
              <el-button
                type="primary"
                icon="el-icon-plus"
                style="margin-left:20px;"
              >父级</el-button>
<!--              @click="openParentAdd"-->
            </div>
          </el-col>
           <el-col :span="2">
                <el-button

                style="margin-left:20px;"
                icon="el-icon-download"
              >导出</el-button>
<!--             @click="downExcel"-->
           </el-col>
        </el-row>

        <p>菜单权限树</p>
        <el-tree
          :data="data"
          show-checkbox
          node-key="id"
          default-expand-all
          :expand-on-click-node="false"
          :render-content="renderContent">
        </el-tree>
      </div>
    </el-card>
    <!-- 节点新增弹出框 -->

    <el-dialog  :visible.sync="addDialogVisible"  width="50%">
      <span>
        <el-form
          size="mini"
          ref="addFormRef"
          label-width="80px"
        >
          <el-form-item label="节点名称" prop="menuName">
            <el-input ></el-input>
          </el-form-item>
          <el-form-item label="URL">
            <el-input ></el-input>
          </el-form-item>
          <el-form-item label="权限编码">
            <el-input></el-input>
          </el-form-item>
          <el-form-item label="图标">
            <el-input></el-input>
          </el-form-item>
          <el-form-item label="是否可用" prop="disabled">
            <template>
              <el-radio label="false">可用</el-radio>
              <el-radio  label="true">禁用</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="是否展开" prop="open">
            <template>
              <el-radio  label="1">展开</el-radio>
              <el-radio  label="0">关闭</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="排序" prop="orderNum">
            <el-input-number  :min="1" :max="10" label="描述文字"></el-input-number>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <template>
              <el-radio  label="0">菜单</el-radio>
              <el-radio  label="1">按钮</el-radio>
            </template>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary"  >确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑节点弹出框 -->
    <el-dialog  :visible.sync="editlogVisible" width="50%" >
      <span>
        <el-form
          size="mini"
          ref="editFormRef"
          label-width="80px"
        >
          <el-form-item label="节点名称" prop="menuName">
            <el-input ></el-input>
          </el-form-item>
          <el-form-item label="URL">
            <el-input ></el-input>
          </el-form-item>
          <el-form-item label="权限编码">
            <el-input ></el-input>
          </el-form-item>
          <el-form-item label="图标">
            <el-input ></el-input>
          </el-form-item>
          <el-form-item label="是否可用" prop="disabled">
            <template>
              <el-radio  :label="false">可用</el-radio>
              <el-radio  :label="true">禁用</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="是否展开" prop="open">
            <template>
              <el-radio  :label="1">展开</el-radio>
              <el-radio  :label="0">关闭</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="排序" prop="orderNum">
            <el-input-number  :min="1" :max="10" label="描述文字"></el-input-number>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <template>
              <el-radio  :label="0">菜单</el-radio>
              <el-radio  :label="1">按钮</el-radio>
            </template>
          </el-form-item>

        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editlogVisible = false">取 消</el-button>
        <el-button
          type="primary"

        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

  let id = 1000;

  export default {
    data() {
      const data = [{
        id: 1,
        label: '一级 1',
        children: [{
          id: 4,
          label: '二级 1-1',
          children: [{
            id: 9,
            label: '三级 1-1-1'
          }, {
            id: 10,
            label: '三级 1-1-2'
          }]
        }]
      }, {
        id: 2,
        label: '一级 2',
        children: [{
          id: 5,
          label: '二级 2-1'
        }, {
          id: 6,
          label: '二级 2-2'
        }]
      }, {
        id: 3,
        label: '一级 3',
        children: [{
          id: 7,
          label: '二级 3-1'
        }, {
          id: 8,
          label: '二级 3-2'
        }]
      }];
      return {
        addDialogVisible: false, //新增节点弹出框
        editlogVisible: false, //编辑节点弹出框
        data: JSON.parse(JSON.stringify(data)),
        data: JSON.parse(JSON.stringify(data))
      }
    },

    methods: {
      append(data) {
        const newChild = { id: id++, label: 'testtest', children: [] };
        if (!data.children) {
          this.$set(data, 'children', []);
        }
        data.children.push(newChild);
      },

      remove(node, data) {
        const parent = node.parent;
        const children = parent.data.children || parent.data;
        const index = children.findIndex(d => d.id === data.id);
        children.splice(index, 1);
      },
      openAdd(data) {
        // this.addTitle = "添加节点 ：当前【" + data.menuName + "】";
        // this.addForm.parentId = data.id;
        // this.pNode = data;
        this.addDialogVisible = true;
      },
       edit(data) {
        this.editlogVisible = true;

        // this.editTitle = "编辑：【" + data.menuName + "】";
        // const { data: res } = await this.$http.get("menu/edit/" + data.id);
        // if (res.code == 200) {
        //   this.editForm = res.data;
        //   this.editlogVisible = true;
        // } else {
        //   return this.$message.error("节点编辑失败:" + res.msg);
        // }
      },
      open() {
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      renderContent(h, { node, data, store }) {
        return (
          <span class="custom-tree-node">
          <span>{node.label}</span>
          <span>
          <el-button
        size="mini"
        type="text"
        on-click={() => {
          this.edit();
          return false;
        }}
      >
      <i class="el-icon-edit"></i>&nbsp;编辑
          </el-button>
          <el-button
        size="mini"
        type="text"
        on-click={() => {
          this.openAdd();
        }}
      >
      <i class="el-icon-plus"></i>&nbsp;增加
          </el-button>

          <el-button
        size="mini"
        type="text"
        on-click={() => {
          this.open();
        }}
      >
      <i class="el-icon-delete"></i>&nbsp;删除
          </el-button>
        </span>
        </span>);
      }
    }
  };
</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
